package jihyun51.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Card_Layout extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final CardLayout card = new CardLayout();
	private JPanel cards = new JPanel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Card_Layout dialog = new Card_Layout();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Card_Layout() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			contentPanel.add(cards, BorderLayout.CENTER);
			cards.setLayout(card);  //2
			//CardLayoout이 적용된 Panel에 2개의 Panel(login,logout)을 카드처럼 포개어 놓음
			//지정한 Panel을 언제든지 보여주기 위해 고유한 이름을 각각 지정 3a, 3b
			{
				JPanel login = new JPanel();
				login.setBackground(new Color(255, 165, 0));
				cards.add(login, "login"); // 3a
			}
			{
				JPanel logout = new JPanel();
				logout.setBackground(new Color(245, 222, 179));
				cards.add(logout, "logout"); //3b
			}
		}
		{
			JPanel btns = new JPanel();
			contentPanel.add(btns, BorderLayout.SOUTH);
			{
				JButton btnNewButton = new JButton("로그인");
				btnNewButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						card.show(cards, "login"); //4a
//						card.next(login); //4b
					}
				});
				btns.add(btnNewButton);
			}
			{
				JButton btnNewButton_1 = new JButton("로그아웃");
				btnNewButton_1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						card.show(cards, "logout"); //4a
//						card.next("logout"); //4b
					}
				});
				btns.add(btnNewButton_1);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

}
